layui.use(['table', 'jquery', 'layer'], function (data) {
    var table = layui.table,
        layer = layui.layer,
        $ = layui.jquery;
    var userName = $('input[name="userName"]').val();
    var isLeader = $('input[name="leader"]').val();
    table.render({
        elem: "#purchaseProjectList",
        height: 'full-125',
        url: ctx + '/supplier/selectParamsPurchaseProject',
        page: true,
        cellMinWidth: 80, // 单元格最小宽度
        toolbar: '#toolbarDemo',
        cols: [[// 表头
            {type: 'checkbox'},
            {type: "numbers"},
            {
                title: "项目编号", templet: function (data) {
                    return data.projectBean.projectNo;
                }
            },
            {
                title: "产品名称", templet: function (data) {
                    return data.projectBean.basicQuotationBean.productName;
                }
            },
            {
                title: "产品数量", templet: function (data) {
                    return data.projectBean.basicQuotationBean.productCount;
                }
            },
            {
                title: "单价(万元)", templet: function (data) {
                    return data.projectBean.basicQuotationBean.productUnitPrice;
                }
            },
            {
                title: "交付日期", templet: function (data) {
                    return data.projectBean.basicQuotationBean.deliveryDate;
                }
            },
            {
                title: "采购负责人", field:"purchaserId" , templet: function (data) {
                    if (data.empBean == null) {
                        return "暂未指派";
                    } else {
                        return data.empBean.empName;
                    }
                }
            },
            {
                title: "状态",  field:"status" ,templet: function (data) {
                    if (data.status == null || data.status == '0') {
                        return "未指派"
                    }
                    if (data.status == 1) {
                        return "已指派"
                    }
                    if (data.status == 2) {
                        return "以申请"
                    }
                    if (data.status == 3) {
                        return  "采购以入库"
                    }
                }
            }, {
                title: "操作", templet: function (data) {
                    if (data.status == 0 && isLeader == 1){
                        return '<a  class="layui-btn layui-btn-primary  layui-btn-xs" lay-event="detailMaterial">详情</a>'
                            + '<a  class="layui-btn layui-btn-primary  layui-btn-xs" lay-event="assign">指派</a>'
                    }
                    if (data.empBean != null) {
                        if (data.empBean.empName.localeCompare(userName) == 0) {
                            return '<a  class="layui-btn layui-btn-primary  layui-btn-xs" lay-event="detailMaterial">详情</a>'
                                + '<a  class="layui-btn layui-btn-primary  layui-btn-xs" lay-event="addPurchaseOrder">下单</a>'
                        } else {
                            return '<a  class="layui-btn layui-btn-primary  layui-btn-xs" lay-event="detailMaterial">详情</a>'
                        }
                    } else {
                        return '<a  class="layui-btn layui-btn-primary  layui-btn-xs" lay-event="detailMaterial">详情</a>'
                    }

                }
            }
        ]]


    })

    // 监听行
    table.on('tool(purchaseProjectFilter)', function (data) {
         if(data.event ==="assign"){
             openAssignPurchaser(data.data);
         }

        if (data.event === 'detailMaterial') {
            openDetailMaterialPage(data.data.projectBean.projectNo);
        }
        if (data.event === 'addPurchaseOrder') {
            openAddPurchaseOrderPage(data.data.projectBean.projectNo)
        }

    })
    //监听头部
    // table.on('toolbar(purchaseProjectFilter)', function (data) {
    //
    //     if (data.event == 'assign') {
    //         var checkStatus = table.checkStatus(data.config.id);
    //         openAssignPurchaser(checkStatus.data);
    //     }
    // })

    function openDetailMaterialPage(projectNo) {
        layer.open({
            type: 2,
            content: ctx + "/supplier/toDetailMaterial?projectNo=" + projectNo,
            area: ['700px', '600px'],
            shadeClose: true,
            maxmin: true,
            title: "物料详情"
        })
    }

    function openAssignPurchaser(data) {
         console.log(data)
         if (data.status !=0){
             layer.msg("已经指派完成了",{icon:6})
             return;
         }
        layer.open({
            type: 2,
            shadeClose: true,
            maxmin: true,
            title: "<h3>采购负责人指派</h3>",
            content: ctx + "/supplier/assignPurchaser?projectId=" + data.projectId,
            area: ['600px', '600px']
        })
    }

    function openAddPurchaseOrderPage(projectNo) {

        layer.open({
            type: 2,
            shadeClose: true,
            maxmin: true,
            title: "<h3>添加采购申请单</h3>",
            content: ctx + "/supplier/addPurchaseOrder?projectNo=" + projectNo,
            area: ['600px', '600px']
        })
    }
})